package com.iretailer.report.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.mapper.MenusMapper;
import com.iretailer.report.model.Menus;
import com.iretailer.report.model.Role;
import com.iretailer.report.model.RoleMenu;
import com.iretailer.report.util.RMap;
import com.iretailer.report.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Auther: xjw
 * @Date: 2019/1/24 11:42
 * @Description:
 */
@Service
public class MenusService extends ServiceImpl<MenusMapper, Menus> {


    @Autowired
    private MenusMapper menusMapper;
    @Autowired
    private RoleMenuService roleMenuService;
    @Autowired
    private RoleService roleService;


    /**
     * 根据 visible 查询
     * 当 visible 为 null 不做限定
     */
    public List<Menus> selectMenusByVisible(String visible) {
        return menusMapper.selectMenusByVisible(visible);
    }

    /**
     * 根据 ids 和 visible 查询出
     */
    public List<Menus> selectMenusByIdsAndVisible(List<String> ids, Integer visible) {
        return menusMapper.selectMenusByIdsAndVisible(ids, visible);
    }

    /**
     * @Author xjw
     * @Date 2019/2/21 17:10
     * @Description : 根据 roleId 返回Menus 的树形结构
     **/
    public List<Menus> selectMenuTreeByRoleId(String roleId) {
        if (StringUtil.notEmpty(roleId)) {
            if (roleId.equals("-")) {
                List<Menus> visibleMenus = selectMenusByVisible("1");
                return convertTree(visibleMenus, "-");
            } else {
                List<Menus> roleMenus = selectMenusByRoleId(roleId);
                return convertTree(roleMenus, "-");
            }
        } else {
            return null;
        }

    }

    /**
     * 树形结构
     */
    public List<Menus> convertTree(List<Menus> menus, String parentId) {
        List<Menus> result = new ArrayList<>();
        if (menus != null && menus.size() > 0) {
            for (Menus m : menus) {
                if (m.getParentId() != null && (m.getParentId()).equals(parentId)) {
                    List<Menus> children = convertTree(menus, m.getId());
                    m.setChildren(children);
                    result.add(m);
                }
            }
        }
        return result;
    }


    /**
     * 根据父 id 查询所有的 menus
     */
    public List<Menus> selectMenusByParentId(String parenId) {
        return menusMapper.selectMenusByParentId(parenId);
    }

    /**
     * @Author xjw
     * @Date 2019/2/11 13:24
     * @Description : 根据父 id 查询所有的 menus 并分页
     **/
    public PageResult<Menus> selectByParentIdPage(String parentId, QueryOption queryOption) {
        Page page = new Page<Menus>(1, Long.MAX_VALUE);
        QueryWrapper<Menus> queryWrapper = new QueryWrapper<>();
        if (parentId != null) {
            queryWrapper.eq("parentId", parentId);
        }
        queryOption.judge(page, queryWrapper);
        queryWrapper.orderByAsc("Ranked");
        menusMapper.selectPage(page, queryWrapper);
        return new PageResult<>(page.getTotal(), page.getRecords());
    }


    /**
     * 新增 或者 更新
     */
    public boolean saveOrUpdate(Menus menus) {
        if (StringUtil.notEmpty(menus.getId())) {
            menusMapper.updateById(menus);
            return true;
        } else {
            menusMapper.insert(menus);
            return true;
        }
    }

    public boolean deleteByIds(List<String> ids) {
        if (ids != null && ids.size() > 0) {
            //先判断是否有子节点
            for (String id : ids) {
                List<Menus> childrens = list(new QueryWrapper<Menus>().lambda().eq(Menus::getParentId, id));
                if (childrens != null && childrens.size() > 0) {
                    return false;
                }
            }
            //如果有子节点就不进行删除
            return removeByIds(ids);
        } else {
            return false;
        }
    }

    /**
     * 根据 id 删除
     */
    public boolean delete(String id) {

        List<Role> roles = roleService.selectByMenusId(id);
        if (roles.size() > 0) {
//            return "已被角色关联不能删除！！"
            return false;
        } else {
            menusMapper.deleteById(id);
            return true;
        }
    }

    /**
     * 根据 roleId 把关联的 MenusId 都查出来
     */
    public List<Menus> selectMenusByRoleId(String roleId) {
        return menusMapper.selectMenusByRoleId(roleId);
    }

}
